﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>菜单编辑</title>
    <script src="/Apm/js/CMSEdit.js"></script>
    <style>
       
    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">
            
        </div>
        <div class="layui-form layui-form-pane edit-form">
            <div id="OneMenuList" class="layui-form-item">
                <label class="layui-form-label">一级菜单</label>
                <div class="layui-input-block">
                    <select id="OneMenu" lay-filter="OneMenu">
                        <option value="">无</option>
                        
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单ID</label>
                <div class="layui-input-block">
                    <input id="ID" type="text" name="ID" required lay-verify="required" placeholder="请输入菜单ID" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">英文名</label>
                <div class="layui-input-block">
                    <input id="MenuEnglish" type="text" name="MenuEnglish" required lay-verify="required" placeholder="请输入菜单英文名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">中文名</label>
                <div class="layui-input-block">
                    <input id="MenuName" type="text" name="MenuName" required lay-verify="required" placeholder="请输入菜单中文名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input id="MenuIcon" type="text" name="MenuIcon" placeholder="请输入菜单图标" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">链接</label>
                <div class="layui-input-block">
                    <input id="MenuUrl" type="text" name="MenuUrl"  placeholder="请输入菜单跳转链接" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">优先级</label>
                <div class="layui-input-block">
                    <input id="MenuHx" type="text" name="MenuHx" required lay-verify="required|number" placeholder="请输入优先级" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item" pane>
                <label class="layui-form-label">显示</label>
                <div class="layui-input-block">
                    <input id="IsDisplay" type="checkbox" name="IsDisplay" lay-text="是|否" value="1" checked lay-skin="switch">
                </div>
            </div>
            
            <div class="layui-form-item" pane>
                <label class="layui-form-label">权限组</label>
                <div id="GroupCodeList" class="layui-input-block">
                    <!--<input id="1" type="checkbox"  title="写作" value="1">-->
                    
                </div>
            </div>

            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>

        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script>
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer,
            form = layui.form;

        var type = MyPublic.getUrlParam('type');
        var id = MyPublic.getUrlParam('id');
        if (id != null) {
            $("#OneMenuList").css('display', 'none');
            $("#ID").val(id);
        }
        else {
            //获取一级菜单
            ajaxget({
                url: 'api/Method/GetOneMenuList?Token=' + MyPublic.getToken(),
                success: function (data) {
                    var count = data.length;
                    var onemenuhtml = '';
                    for (var i = 0; i < count; i++) {
                        onemenuhtml += '<option value="' + data[i].ID + '">' + data[i].MenuName + '</option>';
                    }
                    $("#OneMenu").append(onemenuhtml);
                    form.render('select');
                }
            })
        }
        
        
        switch (type) {
            case 'add':
                $("#title").html('添加菜单');
                ajaxget({
                    url: 'api/Method/GetOneMaxValue?Token=' + MyPublic.getToken(),
                    success: function (data) {
                        $("#ID").val(data);
                    }
                })
                break;
            case 'edit':
                $("#title").html('编辑菜单');
                
                break;
        }
        GetUserGroupList(type, id);
        $("#truebutton").click(function () {
            $("#submit").click();
        })
        //监听select选择
        form.on('select(OneMenu)', function (data) {
            
            var id = data.value;
            if (id == "") {
                ajaxget({
                    url: 'api/Method/GetOneMaxValue?Token=' + MyPublic.getToken(),
                    success: function (data) {
                        $("#ID").val(data);
                    }
                })

            }
            else {
                ajaxget({
                    url: 'api/Method/GetTwoMaxValue?Token=' + MyPublic.getToken() + '&id=' + id,
                    success: function (data) {
                        $("#ID").val(id + data);
                    }
                })
            }
        });      

        //菜单添加或修改
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var json = data.field;

            obj = $("#GroupCodeList input");
            check_val = [];
            for (k in obj) {
                if (obj[k].checked)
                    check_val.push(obj[k].value);
            }
            json.GroupCodeList = check_val.join(",");

            switch (type) {
                case 'add':
                    ajaxpost({
                        url: 'api/Method/MenuCreate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10000") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭   
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })

                    break;
                case 'edit':
                    ajaxpost({
                        url: 'api/Method/MenuUpdate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10001") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭   
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })
                    break;
            }
            
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        

    });
    /**
     * 获取用户分组
     */
    function GetUserGroupList(type,id) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            $.ajax({
                url: ApiURL + 'api/Method/UserGroupList?Token=' + MyPublic.getToken(),
                type: 'get',
                contentType: 'application/json',
                success: function (data) {
                    var count = data.length;
                    var grouphtml = '';
                    for (var i = 0; i < count; i++) {
                        grouphtml += '<input id="' + data[i].GroupCode + '" type="checkbox"  title="' + data[i].ChinaName + '" value="' + data[i].GroupCode + '">';
                    }
                    $("#GroupCodeList").append(grouphtml);
                    form.render('checkbox');
                    if (type === "edit") {
                        BindDataModel(id);
                    }
                },
                error: function () {
                    layer.msg("数据获取失败，请刷新");

                }
            })

        });
    }
    function BindDataModel(guid) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/MethodPublic/MenuModel?ID=' + guid,
                success: function (data) {
                    $("#MenuName").val(data.MenuName);
                    $("#MenuEnglish").val(data.MenuEnglish);
                    $("#MenuUrl").val(data.MenuUrl);
                    $("#MenuIcon").val(data.MenuIcon);
                    $("#MenuHx").val(data.MenuHx);
                    if (data.IsDisplay === 1) {
                        $("#IsDisplay").attr('checked', true);
                    }
                    else {
                        $("#IsDisplay").attr('checked', false);
                    }
                    var GroupCodeList = data.GroupCodeList.split(",");
                    
                    for (var i = 0; i < GroupCodeList.length; i++) {
                        $('#' + GroupCodeList[i]+'').attr('checked', true);
                    }
                    form.render('checkbox');
                    
                }
            })
        });
    }
</script>